/*
  * Mirante Tecnologia
  * Copyright 2010, Mirante Informatica LTDA, 
  * and individual contributors as indicated by the @authors tag
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package com.miranteinfo.seam.jsf.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.core.Interpolator;

import com.miranteinfo.seam.jsf.util.ResourceBundle;

/**
 * Validador de cpf.
 * 
 * @author lucas lins
 *
 */
@Name("com.miranteinfo.CpfValidator")
@BypassInterceptors
@org.jboss.seam.annotations.faces.Validator
public class CpfValidator implements Validator{

	/**
	 * Realiza a validacao de cpj, utilizando o {@link com.miranteinfo.seam.hibernate.validator.domain.validator.impl.CpfValidator}
	 * @param context
	 * @param component
	 * @param value
	 */
	public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
		String cnpj = (String) value;
		
		com.miranteinfo.seam.hibernate.validator.CpfValidator validator = new com.miranteinfo.seam.hibernate.validator.CpfValidator();
		
		if( !validator.isValid( cnpj ) )
		{
			String label = (String)component.getAttributes().get("label");
			if(label != null && !label.trim().equals("")){
				throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, Interpolator.instance().interpolate(ResourceBundle.getBundleMessage("mirseam.validator.cpf_detail"),label), null));
			}else{
				throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundle.getBundleMessage("mirseam.validator.cpf"), null));
			}
		}
	}
	
}
